<?php

use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;

class User extends SentryUserModel
//class User extends Eloquent
{

    /**
     * Indicates if the model should soft delete.
     *
     * @var bool
     */
    protected $softDelete = true;

    /**
     * Array of rules to validate
     *
     * @var array
     */
    protected $validateRules = array(
        'username' => 'required|alpha_dash|unique:users,username|min:4',
        'email' => 'required|email|unique:users,email',
        'password' => 'required|between:8,32',
        'confirm_password' => 'required|same:password',
        'agree' => 'required'
    );

    /**
     *
     * @param type $arrParams
     * @param type $option
     */
    public function validated($attributes, $option = array())
    {
        if (isset($option['create']))
            unset($this->validateRules['agree']);
        if (isset($option['login'])) {
            unset($this->validateRules['email']);
            unset($this->validateRules['agree']);
            unset($this->validateRules['confirm_password']);
        }
        return Validator::make($attributes, $this->validateRules);
    }

    public function saveItem($arrParams, $option = null)
    {
        $row = isset($arrParams['id']) ? $this->find($arrParams['id']) : new User();
        foreach ($arrParams as $key => $value)
            if ($key != "id")
                $row->{$key} = $value;
        $row->save();
        return $row->userid;
    }

    public function getIdByUsername($username)
    {
        $row = $this->where('username', '=', $username)->first();
        try {
            return $row->id;
        } catch (Exception $ex) {
//            die(var_dump($username));
            return new User();
        }

    }

    public static function getUserByUsername($username)
    {
        $row = User::where('username', '=', $username)->first();
        if (!is_null($row))
            return $row;
        else
            return null;
    }

    public function avatar_link()
    {
        if (!empty($this->avatar))
            return route('show-image', $this->avatar);
        else
            return '';
    }

    public function generate_link()
    {
        return Route('thanh-vien-ct', array('id' => $this->id, 'slug' => Str::slug($this->username)));
    }

    public function favouriteLyrics()
    {
        return $this->belongsToMany('Lyric', 'favourites', 'user_id', 'lyric_id');
    }

    public function lessons()
    {
        return $this->hasMany('Lesson', 'user_id');
    }

    public function videos()
    {
        return $this->hasMany('Video', 'user_id');
    }

    public function lyrics()
    {
        return $this->hasMany('Lyric', 'user_id');
    }

    public function followers()
    {
        return $this->belongsToMany('User', 'follows', 'follower_id', 'user_id');
    }

    public function follows()
    {
        return $this->belongsToMany('User', 'follows', 'user_id', 'follower_id');
    }

    public function inCourses()
    {
        return $this->belongsToMany('Category', 'users_categories', 'user_id', 'category_id');
    }

}

?>
